home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / Communications Toolbox / CTB Sample Code 1.0b16 / CTB Sources / Sources 2 / Connection Tool for CTB / cscr.p < prev    next >
Encoding:
Text File  |  1989-10-06  |  6.8 KB  |  279 lines  |  [TEXT/MPS ]

  1. {************************************************************************************
  2. *
  3. *  Project Name:    CMTools
  4. *     File Name:    cscr.p
  5. *       Authors:    Rob Neville, Alex Kazim, Carol Lee, Byron Han
  6. *          Date:    May 17, 1989
  7. *
  8. *   Description:    
  9. *
  10. *************************************************************************************
  11. *
  12. *    Revision History:
  13. *        5/17/89 - Original version by Rob Neville (IIx)
  14. *        6/26/89 - Rev'd for b2 of Comm Toolbox
  15. *
  16. ************************************************************************************}
  17.  
  18. UNIT mycscr;
  19.  
  20. INTERFACE
  21.  
  22. USES
  23.     MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
  24.     FixMath, Script,
  25.     CMIntf,
  26.     ConnectionTool,
  27.     CMTool,
  28.     CMUtil,
  29.     CRMIntf,
  30.     CTBUtils;
  31.  
  32. CONST    
  33.     MAX_RSRC_LEN = 256;
  34.     
  35. function cscr(hConn: ConnHandle; msg: integer;p1,p2,p3: longint): longint;
  36.  
  37. IMPLEMENTATION
  38.  
  39.  
  40. function SetConfig(hConn: ConnHandle; theStr:Ptr;StrResID: integer): INTEGER; FORWARD;
  41. function GetConfigStr(hConn: ConnHandle; StrResID: integer): Ptr; FORWARD;
  42.  
  43.  
  44. {    **************************    }
  45. {    Entry for Script Interface    }
  46. {    **************************    }
  47.  
  48. function cscr(hConn: ConnHandle;msg: integer;p1,p2,p3: longint): longint;
  49. VAR
  50.     saved : SignedByte;
  51.     
  52. begin
  53.     saved := HGetState( handle(hConn) );
  54.     hLock(handle(hConn));
  55.     case msg of
  56.         cmMgetMsg:
  57.             cscr := ord(GetConfigStr( hConn, verUS));
  58.         cmMsetMsg:
  59.             cscr := SetConfig(  hConn, Ptr(p1), verUS);
  60.     end; {case}
  61.     HSetState(handle(hConn), saved);
  62. end; {fscr}
  63.  
  64.  
  65. {    **************************************************    }
  66. {    Takes a piece of a configuration string, parses it,    }
  67. {    and sets the configuration data                        }
  68. {    **************************************************    }
  69. {    returns 0 if no error, -1 for generic errors and    }
  70. {    string index ( would be 1 base ) if the token wasn't recognized }
  71.  
  72. function SetConfig( hConn: ConnHandle; theStr:Ptr;StrResID: integer): INTEGER;
  73. var
  74.     pConfig        : ConfigPtr;
  75.     i, local    : integer;
  76.     myToken        : TokenRecPtr;            {each token}
  77.     theVal        : longint;
  78.     aTokenPtr    : TokenBlockPtr;    {the whole token block}
  79.     returnVal    : longint;
  80.     tempVal,
  81.     tokeIndex,                        {string index for token strings}
  82.     valIndex    : integer;            {string index for value strings}
  83.     tokeStr        : str255;            {token as string}
  84.     procID        : integer ;        
  85.     oldattr        : integer ;    
  86.     charcount    : integer;
  87.     error        : integer ;
  88.     theToken    : tokenType;
  89.     
  90. begin
  91.     {Map to local Resource IDs}
  92.     pConfig := ConfigPtr( hConn^^.config );
  93.     procID := hConn^^.procID ;
  94.     StrResID:= CRMLocalToRealID(ClassCM,procID,'STR#',StrResID);
  95.     if  StrResID = -1 then
  96.     begin
  97.         SetConfig:= -1;
  98.         EXIT(SetConfig);                        {abort, abort}
  99.     end;
  100.     
  101.     returnVal:= InitTokenBlock(aTokenPtr) ;
  102.     if returnVal <> noErr then 
  103.     begin
  104.         SetConfig:= returnVal;
  105.         EXIT(SetConfig);                        {abort, abort}
  106.     end;
  107.     
  108.     returnVal := 1;
  109.     
  110.     aTokenPtr^.source := theStr;                {what to parse}
  111.     aTokenPtr^.sourceLength := strLen(theStr);    {just how long}
  112.     
  113.     {tokenize the string}
  114.     if IntlTokenize(aTokenPtr) <> tokenOK then 
  115.     begin        
  116.         DisposeTokenBlock(aTokenPtr);
  117.         SetConfig:= -1;
  118.         EXIT(SetConfig);
  119.     end;
  120.     
  121.     {for every token}
  122.     for i := 0 to (aTokenPtr^.tokenCount -1) do 
  123.     begin
  124.         theToken := GetSuperToken(aTokenPtr,i,tokeStr);
  125.         if theToken in WhiteTokens then
  126.             cycle
  127.         else if theToken = TokenAlpha then
  128.         begin
  129.             returnVal := 1;
  130.             
  131.             tokeIndex := MatchResString(tokeStr,1,NUMOFSTRING,StrResID);
  132.             if tokeIndex = -1 then
  133.                 leave;
  134.             if not (GetSuperToken(aTokenPtr,i,tokeStr) in WhiteTokens) then
  135.                 leave;
  136.             case tokeIndex of
  137.             
  138.                 BYRON_ID: 
  139.                     if GetSuperToken(aTokenPtr,i,tokeStr) = tokenAlpha then
  140.                     begin
  141.                         valIndex := MatchResString(tokeStr,TRUE_ID,FALSE_ID,StrResID);
  142.                         if valIndex <> -1 then
  143.                         begin
  144.                             returnVal := 0;
  145.                             if valIndex = FALSE_ID then
  146.                                 pConfig^.param1 := FALSE
  147.                             else if valIndex = TRUE_ID then
  148.                                 pConfig^.param1 := TRUE
  149.                             else
  150.                                 returnVal := 1;
  151.                         end;
  152.                     end;{ BYRON_ID}
  153.                     
  154.                 ROB_ID:
  155.                 if GetSuperToken(aTokenPtr,i,tokeStr) = tokenAlpha then
  156.                     begin
  157.                         valIndex := MatchResString(tokeStr,TRUE_ID,FALSE_ID,StrResID);
  158.                         if valIndex <> -1 then
  159.                         begin
  160.                             returnVal := 0;
  161.                             if valIndex = FALSE_ID then
  162.                                 pConfig^.param2 := FALSE
  163.                             else if valIndex = TRUE_ID then
  164.                                 pConfig^.param2 := TRUE
  165.                             else
  166.                                 returnVal := 1;
  167.                         end;
  168.                     end; { ROB_ID }
  169.                     
  170.             end; {case}
  171.             if  returnVal = 1 then
  172.                     leave ;
  173.         end {An Alpha Token}
  174.         else
  175.             leave ;        { abort on error }
  176.     end; {for every token}
  177.     if  returnVal = 1 then
  178.     begin
  179.         { get the first character position for the unrecognized token }
  180.         i := i - 1;
  181.         returnVal := 0 ;
  182.         for charcount := 1 TO i DO
  183.         begin
  184.             myToken := TokenRecPtr(ord(aTokenPtr^.tokenList) + (charcount-1)*sizeOf(TokenRec));
  185.             returnVal := returnVal + myToken^.length;
  186.         end ;
  187.         returnVal := returnVal + 1 ;        { because the string index is 1 base }
  188.     end; 
  189.     
  190.     DisposeTokenBlock(aTokenPtr);
  191.     SetConfig:= returnVal;            {g'day, mate}
  192. end; {SetConfig}
  193.  
  194.  
  195.  
  196. {    *************************************************************    }
  197. {    Reads thru the configuration data and returns a null- termiated }
  198. {    config string, returns zero if error occurs                        }
  199. {    *************************************************************    }
  200.  
  201. function GetConfigStr(hConn: ConnHandle; StrResID: integer): Ptr;
  202. var
  203.     configStr    : Ptr;        {string to return}
  204.     tempPtr        : Ptr;
  205.     pConfig        : ConfigPtr;
  206.     theString    : Str255;
  207.     i            : integer;
  208.     anyErr        : integer;
  209.     valIndex    : integer;
  210.     procID        : integer;
  211.     totalLen    : longint;
  212.     notDone        : Boolean;
  213.     firstPass    : Boolean;
  214.     configHdl    : Handle;
  215.     savedConfigStr    : Ptr;
  216.  
  217. begin
  218.     procID := hConn^^.procID ;
  219.     pConfig := ConfigPtr(hConn^^.config);
  220.     notDone := TRUE;
  221.     firstPass := TRUE;
  222.     
  223.     StrResID:= CRMLocalToRealID(ClassCM,procID,'STR#',StrResID);
  224.     if StrResID = -1 then
  225.     begin
  226.         GetConfigStr := Ptr(-1 );
  227.         EXIT(GetConfigStr);
  228.     end ;
  229.     while notDone do
  230.     begin
  231.         totalLen := 0;
  232.         for i := 1 to NUMOFSTRING do
  233.         begin
  234.             GetIndString(theString,StrResID,i);
  235.             if theString[0] = chr(0) then
  236.                 leave;
  237.             totalLen := totalLen + MyCat(configStr,theString,firstPass);
  238.             case i of
  239.                 BYRON_ID:
  240.                     begin
  241.                         if pConfig^.param1 = TRUE then
  242.                             GetIndString(theString,StrResID,TRUE_ID)
  243.                         else
  244.                             GetIndString(theString,StrResID,FALSE_ID);
  245.                         totalLen := totalLen + MyCat(configStr,theString,firstPass);
  246.                     end;
  247.                 ROB_ID:
  248.                     begin
  249.                         if pConfig^.param2 = TRUE then
  250.                             GetIndString(theString,StrResID,TRUE_ID)
  251.                         else
  252.                             GetIndString(theString,StrResID,FALSE_ID);
  253.                         totalLen := totalLen + MyCat(configStr,theString,firstPass);
  254.                     end;
  255.             end; {case}
  256.         end; {for}
  257.         if firstPass then
  258.         begin
  259.             firstPass := false;
  260.             configStr := NewPtr (totalLen);
  261.             if configStr = NIL then
  262.             begin
  263.                 GetConfigStr := nil;
  264.                 EXIT(GetConfigStr);
  265.             end;
  266.             savedconfigStr := configStr;
  267.         end
  268.         else
  269.         begin
  270.             notDone := false;
  271.         end;
  272.     end; {while notdone}
  273.     
  274.     configStr := Ptr(ord4(configStr)-1);
  275.     configStr^ := 0;
  276.     GetConfigStr := savedconfigStr;
  277. end; {GetConfigStr}
  278.  
  279. END.